package Teacher.Examination;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.JButton;
import javax.swing.JComboBox;

import Database.CallDB;
import Database.CourseMgmt;
import Database.MyTableModel;
import Exception.CheckDataException;
import Exception.ComboModelException;
import Exception.FormatScrollPaneException;
import Exception.LoadDriverException;
import General.GeneralInformation;
import General.GeneralMethod;
import General.GraphicsSetting;
import General.ManagementPanel;
import General.TreatError;
import GenericMethod.ComboBoxMethod;

public class PanelLaunchExamination extends ManagementPanel
{
	private JButton buttonReady;
	private JComboBox<Object> comboPenalty, comboExam;
	private ComboBoxMethod comboBoxMethod;
	private int idPenaltyForLaunch, idExamForLaunch;
	
	public PanelLaunchExamination(CallDB callDB, Connection connection) throws CheckDataException, FormatScrollPaneException, ComboModelException, LoadDriverException
	{
		super(callDB, connection);
		
		//Disable Component
		getTableLeft().setRowSelectionAllowed(false);
		getTableLeft().setFocusable(false);
		getButtonCreate().setVisible(false);
		getButtonCreate().setEnabled(false);
		getButtonModify().setVisible(false);
		getButtonModify().setEnabled(false);
		getButtonDelete().setVisible(false);
		
		creationComponent();
		showComponent();
	}
	
	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{			
			try
			{
				if(arg0.getSource() == comboPenalty)
				{
					GeneralInformation.createPenaltyModel(connection);
					int id = (int)GeneralInformation.getPenaltyModel().getValueAt(comboPenalty.getSelectedIndex(), 2);
					idPenaltyForLaunch = id;
					
					GeneralInformation.createPenaltyDescriptModel(connection, id);
					GeneralMethod.setLeftModel(GeneralInformation.getPenaltyDescriptModel());
					
					refreshSuperLeftTable();
				}
				else if(arg0.getSource() == comboExam)
				{
					GeneralInformation.createExamModel(connection);
					int id = (int)GeneralInformation.getExamModel().getValueAt(comboExam.getSelectedIndex(), 2);
					idExamForLaunch = id;
					
					GeneralInformation.setIdExaminationLaunch(idExamForLaunch);
					
					GeneralInformation.createStudentListModel(connection, id);
					GeneralMethod.setDescriptModel(GeneralInformation.getStudentListModel());
					
					refreshSuperDescriptTable();
				}
			}
			catch(CheckDataException | FormatScrollPaneException | LoadDriverException error)
			{
				TreatError.showError(error);
			}
		}
	}
	
	public void creationComponent() throws CheckDataException, ComboModelException, LoadDriverException
	{
		MyActionListener actionListener = new MyActionListener();
		comboBoxMethod = new ComboBoxMethod();
		
		comboPenalty = new JComboBox<Object>();
		comboPenalty.setName("comboPenalty");
		
		comboExam = new JComboBox<Object>();
		comboExam.setName("comboExam");

		GeneralInformation.createPenaltyModel(connection);
		comboBoxMethod.comboModel(comboPenalty, GeneralInformation.getPenaltyModel(), "column", 0); //Set the Generic Left JTable by the MyTableModel

		GeneralInformation.createExamModel(connection);
		comboBoxMethod.comboModel(comboExam, GeneralInformation.getExamModel(), "column", 0);
		
		buttonReady = new JButton("READY");
		buttonReady.setName("Start");
		
		comboExam.addActionListener(actionListener);
		comboPenalty.addActionListener(actionListener);
		buttonReady.addActionListener(actionListener);
	}

	public void showComponent() throws FormatScrollPaneException
	{
		comboPenalty.setBounds(50, 380, 180, 30);
		comboExam.setBounds(560, 380, 180, 30);
		buttonReady.setBounds(290, 380, 210, 98);
		
		refreshSuperDescriptTable();
		tableDescript.setCellSelectionEnabled(false);
		tableDescript.setFocusable(false);
		
		this.add(comboExam);
		this.add(comboPenalty);
		this.add(buttonReady);
		
		GraphicsSetting.setBackground(this, "image/Bg-Main.jpg", 0, 0, 800, 600);
	}
	
	public JButton getButtonReady(){return buttonReady;}
	public int getIdPenaltyForLaunch(){return idPenaltyForLaunch;}
	public int getIdExamForLaunch(){return idExamForLaunch;}
}